package org.znxs.comfriend.adapters;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import org.znxs.comfriend.R;
import org.znxs.comfriend.activities.ImageDetailActivity;

import java.io.File;
import java.util.List;

public class ImageListAdapter extends RecyclerView.Adapter<ImageListAdapter.ImageViewHolder> {
    private Context context;
    private List<?> imageItems; // 支持多种类型的数据

    public ImageListAdapter(Context context, List<?> imageItems) {
        this.context = context;
        this.imageItems = imageItems;
    }

    @NonNull
    @Override
    public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_image, parent, false);
        return new ImageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {
        Object item = imageItems.get(position);
        Context context = holder.itemView.getContext();

        if (item instanceof Integer) { // 如果是资源 ID
            holder.imageView.setImageResource((Integer) item);
        } else if (item instanceof String) { // 如果是URL或文件路径
            String pathOrUrl = (String) item;
            if (pathOrUrl.startsWith("http")) { // 判断是否为网络URL
                Glide.with(context).load(pathOrUrl).into(holder.imageView);
            } else { // 本地文件路径
                File file = new File(pathOrUrl);
                if (file.exists()) { // 确保文件存在
                    Glide.with(context)
                            .load(file)
                            .placeholder(R.drawable.pengyiyan) // 加载中的占位图
                            .error(R.drawable.error) // 加载失败的占位图
                            .into(holder.imageView);
                } else {
                    holder.imageView.setImageResource(R.drawable.error); // 文件不存在时显示错误图片
                }
            }
        } else {
            holder.imageView.setImageResource(R.drawable.error); // 默认占位图
        }

        holder.itemView.setOnClickListener(v -> {
            Intent intent = new Intent(context, ImageDetailActivity.class);

            if (item instanceof Integer) {
                intent.putExtra("image_res_id", (Integer) item);
            } else if (item instanceof String) {
                intent.putExtra("image_url_or_path", (String) item);
            }

            // 确保 context 是一个 Activity 或者使用 context.startActivity(intent)
            // 如果 context 不是 Activity，你可能需要这样做：
            if (!(context instanceof Activity)) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
            context.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return imageItems.size();
    }

    static class ImageViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;

        public ImageViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.iv_image);
        }
    }
}